package com.linwen.javase.thread;

/**
 * @author : linwen
 * @date : 下午10:37 2021/1/11
 * 继承Thread类用 同步方法
 */
public class 线程锁4 {
    public static void main(String[] args) {
        Window4 window1 = new Window4();
        Window4 window2 = new Window4();
        Window4 window3 = new Window4();
        window1.setName("窗口1");
        window2.setName("窗口2");
        window3.setName("窗口3");
        window1.start();
        window2.start();
        window3.start();

    }
}
class Window4 extends Thread{
    private static int ticket = 100;

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sell();
        }
    }
//继承只能将同步方法定义为静态的，做到同步锁为Window4.class
    private static synchronized  void sell() {
        if(ticket>0){

            System.out.println(Thread.currentThread().getName() + "==" + ticket);
            ticket--;
        }else{
            System.exit(0);
        }
    }
}
